home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / ohlutil.zip / DD.C < prev    next >
C/C++ Source or Header  |  1990-06-16  |  21KB  |  785 lines

  1. /* dd -- convert and copy files.
  2.    Copyright (C) 1985, 1989, 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Written by Paul Rubin and David MacKenzie. */
  19.  
  20. /* Options:
  21.  
  22.    Numbers can be followed by a multiplier:
  23.    b=512, k=1024, w=2, xm=number m
  24.  
  25.    if=FILE            Read from FILE instead of stdin.
  26.    of=FILE            Write to FILE instead of stdout.
  27.    ibs=BYTES            Read BYTES bytes at a time.
  28.    obs=BYTES            Write BYTES bytes at a time.
  29.    bs=BYTES            Override ibs and obs.
  30.    cbs=BYTES            Convert BYTES bytes at a time.
  31.    skip=BLOCKS            Skip BLOCKS ibs-sized blocks at
  32.                 start of input.
  33.    seek=BLOCKS            Skip BLOCKS obs-sized blocks at
  34.                 start of output.
  35.    count=BLOCKS            Copy only BLOCKS input blocks.
  36.    conv=CONVERSION[,CONVERSION...]
  37.  
  38.    Conversions:
  39.    ascii            Convert EBCDIC to ASCII.
  40.    ebcdic            Convert ASCII to EBCDIC.
  41.    ibm                Convert ASCII to alternate EBCDIC.
  42.    block            Pad newline-terminated records to size of
  43.                 cbs, replacing newline with trailing spaces.
  44.    unblock            Replace trailing spaces in cbs-sized block
  45.                 with newline.
  46.    lcase            Change uppercase characters to lowercase.
  47.    ucase            Change lowercase characters to uppercase.
  48.    swab                Swap every pair of bytes.
  49.    noerror            Continue after read and write errors.
  50.    sync                Pad every input block to size of ibs with
  51.                 trailing spaces. */
  52.  
  53. #include <stdio.h>
  54. #include <ctype.h>
  55. #include <sys/types.h>
  56. #include <signal.h>
  57. #include "system.h"
  58. #ifdef STDC_HEADERS
  59. #include <errno.h>
  60. #include <stdlib.h>
  61. #else
  62. char *malloc ();
  63.  
  64. extern int errno;
  65. #endif
  66.  
  67. #ifndef _POSIX_SOURCE
  68. long lseek ();
  69. #endif
  70.  
  71. #define equal(p, q) (strcmp ((p),(q)) == 0)
  72. #define max(a, b) ((a) > (b) ? (a) : (b))
  73.  
  74. /* Default input and output blocksize. */
  75. #define BLOCKSIZE 512
  76.  
  77. /* Conversions bit masks. */
  78. #define C_ASCII 01
  79. #define C_EBCDIC 02
  80. #define C_IBM 04
  81. #define C_BLOCK 010
  82. #define C_UNBLOCK 020
  83. #define C_LCASE 040
  84. #define C_UCASE 0100
  85. #define C_SWAB 0200
  86. #define C_NOERROR 0400
  87. #define C_SYNC 01000
  88. #define C_HARDWAY 04000        /* Use separate input and output buffers. */
  89.  
  90. char *xmalloc ();
  91. SIGTYPE interrupt_handler ();
  92. int bit_count ();
  93. int parse_integer ();
  94. void copy ();
  95. void error ();
  96. void parse_conversion ();
  97. void quit ();
  98. void scanargs ();
  99. void usage ();
  100.  
  101. /* The name this program was run with. */
  102. char *program_name;
  103.  
  104. /* The name of the input file, or NULL for the standard input. */
  105. char *input_file = NULL;
  106.  
  107. /* The input file descriptor. */
  108. int input_fd = 0;
  109.  
  110. /* The name of the output file, or NULL for the standard output. */
  111. char *output_file = NULL;
  112.  
  113. /* The output file descriptor. */
  114. int output_fd = 1;
  115.  
  116. /* The number of bytes in which atomic reads are done. */
  117. long input_blocksize = BLOCKSIZE;
  118.  
  119. /* The number of bytes in which atomic writes are done. */
  120. long output_blocksize = BLOCKSIZE;
  121.  
  122. /* Conversion buffer size, in bytes. */
  123. long conversion_blocksize = BLOCKSIZE;
  124.  
  125. /* Skip this many records of `input_blocksize' bytes on input. */
  126. int skip_records = 0;
  127.  
  128. /* Seek to this record of `output_blocksize' bytes on output. */
  129. long seek_record = 0;
  130.  
  131. /* Copy only this many records.  <0 means no limit. */
  132. int max_record = -1;
  133.  
  134. /* Bit vector of conversions to apply. */
  135. int conversions_mask = 0;
  136.  
  137. /* Number of partial blocks written. */
  138. unsigned w_partial = 0;
  139.  
  140. /* Number of full blocks written. */
  141. unsigned w_full = 0;
  142.  
  143. /* Number of partial blocks read. */
  144. unsigned r_partial = 0;
  145.  
  146. /* Number of full blocks read. */
  147. unsigned r_full = 0;
  148.  
  149. /* Records truncated by conv=block. */
  150. unsigned r_truncate = 0;
  151.  
  152. /* Output representation of newline and space characters. */
  153. int newline_character = '\n';
  154. int space_character = ' ';
  155.  
  156. struct conversion
  157. {
  158.   char *convname;
  159.   int conversion;
  160. };
  161.  
  162. struct conversion conversions[] =
  163. {
  164.   "ascii", C_ASCII | C_HARDWAY,    /* ebcdic to ascii */
  165.   "ebcdic", C_EBCDIC | C_HARDWAY,    /* ascii to ebcdic */
  166.   "ibm", C_IBM | C_HARDWAY,    /* slightly different atoe */
  167.   "block", C_BLOCK | C_HARDWAY,    /* var to fixed len recs */
  168.   "unblock", C_UNBLOCK | C_HARDWAY,    /* fixed to var */
  169.   "lcase", C_LCASE | C_HARDWAY,    /* translate upper to lower */
  170.   "ucase", C_UCASE | C_HARDWAY,    /* translate lower to upper */
  171.   "swab", C_SWAB | C_HARDWAY,    /* swap bytes of input */
  172.   "noerror", C_NOERROR,        /* ignore i/o errors */
  173.   "sync", C_SYNC,        /* pad input recs to ibs */
  174.   NULL, 0
  175. };
  176.  
  177. /* Translation table formed by applying successive transformations. */
  178. unsigned char trans_table[256];
  179.  
  180. unsigned char ascii_to_ebcdic[] =
  181. {
  182.   0, 01, 02, 03, 067, 055, 056, 057,
  183.   026, 05, 045, 013, 014, 015, 016, 017,
  184.   020, 021, 022, 023, 074, 075, 062, 046,
  185.   030, 031, 077, 047, 034, 035, 036, 037,
  186.   0100, 0117, 0177, 0173, 0133, 0154, 0120, 0175,
  187.   0115, 0135, 0134, 0116, 0153, 0140, 0113, 0141,
  188.   0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
  189.   0370, 0371, 0172, 0136, 0114, 0176, 0156, 0157,
  190.   0174, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
  191.   0310, 0311, 0321, 0322, 0323, 0324, 0325, 0326,
  192.   0327, 0330, 0331, 0342, 0343, 0344, 0345, 0346,
  193.   0347, 0350, 0351, 0112, 0340, 0132, 0137, 0155,
  194.   0171, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
  195.   0210, 0211, 0221, 0222, 0223, 0224, 0225, 0226,
  196.   0227, 0230, 0231, 0242, 0243, 0244, 0245, 0246,
  197.   0247, 0250, 0251, 0300, 0152, 0320, 0241, 07,
  198.   040, 041, 042, 043, 044, 025, 06, 027,
  199.   050, 051, 052, 053, 054, 011, 012, 033,
  200.   060, 061, 032, 063, 064, 065, 066, 010,
  201.   070, 071, 072, 073, 04, 024, 076, 0341,
  202.   0101, 0102, 0103, 0104, 0105, 0106, 0107, 0110,
  203.   0111, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
  204.   0130, 0131, 0142, 0143, 0144, 0145, 0146, 0147,
  205.   0150, 0151, 0160, 0161, 0162, 0163, 0164, 0165,
  206.   0166, 0167, 0170, 0200, 0212, 0213, 0214, 0215,
  207.   0216, 0217, 0220, 0232, 0233, 0234, 0235, 0236,
  208.   0237, 0240, 0252, 0253, 0254, 0255, 0256, 0257,
  209.   0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
  210.   0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
  211.   0312, 0313, 0314, 0315, 0316, 0317, 0332, 0333,
  212.   0334, 0335, 0336, 0337, 0352, 0353, 0354, 0355,
  213.   0356, 0357, 0372, 0373, 0374, 0375, 0376, 0377
  214. };
  215.  
  216. unsigned char ascii_to_ibm[] =
  217. {
  218.   0, 01, 02, 03, 067, 055, 056, 057,
  219.   026, 05, 045, 013, 014, 015, 016, 017,
  220.   020, 021, 022, 023, 074, 075, 062, 046,
  221.   030, 031, 077, 047, 034, 035, 036, 037,
  222.   0100, 0132, 0177, 0173, 0133, 0154, 0120, 0175,
  223.   0115, 0135, 0134, 0116, 0153, 0140, 0113, 0141,
  224.   0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
  225.   0370, 0371, 0172, 0136, 0114, 0176, 0156, 0157,
  226.   0174, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
  227.   0310, 0311, 0321, 0322, 0323, 0324, 0325, 0326,
  228.   0327, 0330, 0331, 0342, 0343, 0344, 0345, 0346,
  229.   0347, 0350, 0351, 0255, 0340, 0275, 0137, 0155,
  230.   0171, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
  231.   0210, 0211, 0221, 0222, 0223, 0224, 0225, 0226,
  232.   0227, 0230, 0231, 0242, 0243, 0244, 0245, 0246,
  233.   0247, 0250, 0251, 0300, 0117, 0320, 0241, 07,
  234.   040, 041, 042, 043, 044, 025, 06, 027,
  235.   050, 051, 052, 053, 054, 011, 012, 033,
  236.   060, 061, 032, 063, 064, 065, 066, 010,
  237.   070, 071, 072, 073, 04, 024, 076, 0341,
  238.   0101, 0102, 0103, 0104, 0105, 0106, 0107, 0110,
  239.   0111, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
  240.   0130, 0131, 0142, 0143, 0144, 0145, 0146, 0147,
  241.   0150, 0151, 0160, 0161, 0162, 0163, 0164, 0165,
  242.   0166, 0167, 0170, 0200, 0212, 0213, 0214, 0215,
  243.   0216, 0217, 0220, 0232, 0233, 0234, 0235, 0236,
  244.   0237, 0240, 0252, 0253, 0254, 0255, 0256, 0257,
  245.   0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
  246.   0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
  247.   0312, 0313, 0314, 0315, 0316, 0317, 0332, 0333,
  248.   0334, 0335, 0336, 0337, 0352, 0353, 0354, 0355,
  249.   0356, 0357, 0372, 0373, 0374, 0375, 0376, 0377
  250. };
  251.  
  252. unsigned char ebcdic_to_ascii[] =
  253. {
  254.   0, 01, 02, 03, 0234, 011, 0206, 0177,
  255.   0227, 0215, 0216, 013, 014, 015, 016, 017,
  256.   020, 021, 022, 023, 0235, 0205, 010, 0207,
  257.   030, 031, 0222, 0217, 034, 035, 036, 037,
  258.   0200, 0201, 0202, 0203, 0204, 012, 027, 033,
  259.   0210, 0211, 0212, 0213, 0214, 05, 06, 07,
  260.   0220, 0221, 026, 0223, 0224, 0225, 0226, 04,
  261.   0230, 0231, 0232, 0233, 024, 025, 0236, 032,
  262.   040, 0240, 0241, 0242, 0243, 0244, 0245, 0246,
  263.   0247, 0250, 0133, 056, 074, 050, 053, 041,
  264.   046, 0251, 0252, 0253, 0254, 0255, 0256, 0257,
  265.   0260, 0261, 0135, 044, 052, 051, 073, 0136,
  266.   055, 057, 0262, 0263, 0264, 0265, 0266, 0267,
  267.   0270, 0271, 0174, 054, 045, 0137, 076, 077,
  268.   0272, 0273, 0274, 0275, 0276, 0277, 0300, 0301,
  269.   0302, 0140, 072, 043, 0100, 047, 075, 042,
  270.   0303, 0141, 0142, 0143, 0144, 0145, 0146, 0147,
  271.   0150, 0151, 0304, 0305, 0306, 0307, 0310, 0311,
  272.   0312, 0152, 0153, 0154, 0155, 0156, 0157, 0160,
  273.   0161, 0162, 0313, 0314, 0315, 0316, 0317, 0320,
  274.   0321, 0176, 0163, 0164, 0165, 0166, 0167, 0170,
  275.   0171, 0172, 0322, 0323, 0324, 0325, 0326, 0327,
  276.   0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337,
  277.   0340, 0341, 0342, 0343, 0344, 0345, 0346, 0347,
  278.   0173, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
  279.   0110, 0111, 0350, 0351, 0352, 0353, 0354, 0355,
  280.   0175, 0112, 0113, 0114, 0115, 0116, 0117, 0120,
  281.   0121, 0122, 0356, 0357, 0360, 0361, 0362, 0363,
  282.   0134, 0237, 0123, 0124, 0125, 0126, 0127, 0130,
  283.   0131, 0132, 0364, 0365, 0366, 0367, 0370, 0371,
  284.   060, 061, 062, 063, 064, 065, 066, 067,
  285.   070, 071, 0372, 0373, 0374, 0375, 0376, 0377
  286. };
  287.  
  288. void
  289. main (argc, argv)
  290.      int argc;
  291.      char **argv;
  292. {
  293.   int i;
  294.  
  295.   program_name = argv[0];
  296.  
  297.   /* Initialize translation table to identity translation. */
  298.   for (i = 0; i < 256; i++)
  299.     trans_table[i] = i;
  300.  
  301.   /* Decode arguments. */
  302.   scanargs (argc, argv);
  303.  
  304.   if (input_file != NULL)
  305.     {
  306.       input_fd = open (input_file, 0);
  307.       if (input_fd < 0)
  308.     error (1, errno, "%s", input_file);
  309.     }
  310.   else
  311.     input_file = "standard input";
  312.  
  313.   if (output_file != NULL)
  314.     {
  315.       output_fd = creat (output_file, 0666);
  316.       if (output_fd < 0)
  317.     error (1, errno, "%s", output_file);
  318.     }
  319.   else
  320.     output_file = "standard output";
  321.   
  322.   signal (SIGINT, interrupt_handler);
  323.   copy ();
  324.   quit (0);
  325. }
  326.  
  327. void
  328. copy ()
  329. {
  330.   struct stat stats;
  331.   register unsigned char c;    /* Each char being converted. */
  332.   register int i;        /* Index into `ibuf'. */
  333.   int oc = 0;            /* Index into `obuf'. */
  334.   int spaces = 0;        /* Number of pending blanks to add. */
  335.   int col = 0;            /* Index into each line. */
  336.   unsigned char *ibuf, *obuf;    /* Buffers. */
  337.   int nread, nwritten;        /* Byte counts for each block. */
  338.  
  339.   ibuf = (unsigned char *) xmalloc (input_blocksize);
  340.   if (conversions_mask & C_HARDWAY)
  341.     obuf = (unsigned char *) xmalloc (output_blocksize);
  342.   else
  343.     obuf = ibuf;
  344.  
  345.   /* Use fstat instead of checking for errno == ESPIPE because
  346.      lseek doesn't work on some special files but doesn't return an
  347.      error, either. */
  348.   if (fstat (input_fd, &stats))
  349.     {
  350.       error (0, errno, "%s", input_file);
  351.       quit (1);
  352.     }
  353.   if ((stats.st_mode & S_IFMT) == S_IFREG)
  354.     {
  355.       if (lseek (input_fd, skip_records * input_blocksize, 0) < 0)
  356.     {
  357.       error (0, errno, "%s", input_file);
  358.       quit (1);
  359.     }
  360.     }
  361.   else
  362.     {
  363.       for (i = 0; i < skip_records; i++)
  364.     if (read (input_fd, ibuf, input_blocksize) < 0)
  365.       {
  366.         error (0, errno, "%s", input_file);
  367.         quit (1);
  368.       }
  369.     }
  370.  
  371.   if (fstat (output_fd, &stats))
  372.     {
  373.       error (0, errno, "%s", output_file);
  374.       quit (1);
  375.     }
  376.   if ((stats.st_mode & S_IFMT) == S_IFREG)
  377.     {
  378.       if (lseek (output_fd, seek_record * output_blocksize, 0) < 0)
  379.     {
  380.       error (0, errno, "%s", output_file);
  381.       quit (1);
  382.     }
  383.     }
  384.   
  385.   if (max_record >= 0 && r_partial + r_full >= max_record)
  386.     return;
  387.  
  388.   while ((max_record < 0 || r_partial + r_full < max_record)
  389.      && (nread = read (input_fd, ibuf, input_blocksize)) != 0)
  390.     {
  391.       if (nread < 0)
  392.     {
  393.       error (0, errno, "%s", input_file);
  394.       if (conversions_mask & C_NOERROR)
  395.         continue;
  396.       else
  397.         quit (2);
  398.     }
  399.  
  400.       if (nread < input_blocksize)
  401.     {
  402.       r_partial++;
  403.       if (conversions_mask & C_SYNC)
  404.         while (nread < input_blocksize)
  405.           ibuf[nread++] = space_character;
  406.     }
  407.       else
  408.     r_full++;
  409.  
  410.       /* If blocksizes are the same and no conversion, just flush. */
  411.       if (ibuf == obuf)
  412.     {
  413.       nwritten = write (output_fd, obuf, nread);
  414.       if (nwritten != nread)
  415.         {
  416.           error (0, errno, "%s", output_file);
  417.           if (nwritten > 0)
  418.         w_partial++;
  419.           if (!(conversions_mask & C_NOERROR))
  420.         quit (1);
  421.         }
  422.       else if (nread == input_blocksize)
  423.         w_full++;
  424.       else
  425.         w_partial++;
  426.       continue;
  427.     }
  428.  
  429.       /* Copy input to output, doing conversion and flushing output
  430.      as necessary. */
  431.       for (i = 0; i < nread; i++)
  432.     {
  433.       c = trans_table[ibuf[i]];
  434.  
  435.       if (conversions_mask & C_BLOCK)
  436.         {
  437.           if (spaces > 0)
  438.         {
  439.           spaces--;
  440.           i--;        /* Push char back; get it next time. */
  441.           c = space_character;
  442.         }
  443.           else if (c == newline_character)
  444.         {
  445.           spaces = max (0, conversion_blocksize - col);
  446.           col = 0;
  447.           continue;
  448.         }
  449.           else if (col++ >= conversion_blocksize)
  450.         {
  451.           r_truncate++;
  452.           continue;
  453.         }
  454.         }
  455.       else if (conversions_mask & C_UNBLOCK)
  456.         {
  457.           if (col++ >= conversion_blocksize)
  458.         {
  459.           col = spaces = 0;
  460.           i--;        /* Push char back; get it next time. */
  461.           c = newline_character;
  462.         }
  463.           else if (c == space_character)
  464.         {
  465.           spaces++;
  466.           continue;
  467.         }
  468.           else if (spaces > 0)
  469.         {
  470.           /* Hit the end of a run of spaces that were not at the
  471.              end of the conversion buffer. */
  472.           spaces--;
  473.           col--;
  474.           i--;
  475.           c = space_character;
  476.         }
  477.         }
  478.  
  479.       if (conversions_mask & C_SWAB)
  480.         obuf[oc++ ^ 1] = c;
  481.       else
  482.         obuf[oc++] = c;
  483.  
  484.       if (oc >= output_blocksize)
  485.         {
  486.           nwritten = write (output_fd, obuf, output_blocksize);
  487.           if (nwritten != output_blocksize)
  488.         {
  489.           error (0, errno, "%s", output_file);
  490.           if (nwritten > 0)
  491.             w_partial++;
  492.           if (!(conversions_mask & C_NOERROR))
  493.             quit (1);
  494.         }
  495.           else
  496.         w_full++;
  497.           oc = 0;
  498.         }
  499.     }
  500.     }
  501.  
  502.   if (conversions_mask & C_BLOCK)
  503.     {
  504.       /* The last record might need to be padded. */
  505.       while (spaces-- > 0)
  506.     {
  507.       if (conversions_mask & C_SWAB)
  508.         obuf[oc++ ^ 1] = space_character;
  509.       else
  510.         obuf[oc++] = space_character;
  511.       if (oc >= output_blocksize)
  512.         {
  513.           nwritten = write (output_fd, obuf, output_blocksize);
  514.           if (nwritten != output_blocksize)
  515.         {
  516.           error (0, errno, "%s", output_file);
  517.           if (nwritten > 0)
  518.             w_partial++;
  519.           if (!(conversions_mask & C_NOERROR))
  520.             quit (1);
  521.         }
  522.           else
  523.         w_full++;
  524.           oc = 0;
  525.         }
  526.     }
  527.     }
  528.   else if (conversions_mask & C_UNBLOCK)
  529.     {
  530.       /* Add a final '\n' if there are exactly `conversion_blocksize'
  531.      characters in the final record. */
  532.       if (col == conversion_blocksize)
  533.     {
  534.       if (conversions_mask & C_SWAB)
  535.         obuf[oc++ ^ 1] = newline_character;
  536.       else
  537.         obuf[oc++] = newline_character;
  538.     }
  539.     }
  540.  
  541.   /* Flush last block. */
  542.   if (oc > 0)
  543.     {
  544.       /* First, fix earlier screw-up if swapping bytes and `oc' is odd. */
  545.       if ((conversions_mask & C_SWAB) && (oc & 1))
  546.     obuf[oc - 1] = obuf[oc];
  547.  
  548.       nwritten = write (output_fd, obuf, oc);
  549.       if (nwritten > 0)
  550.     w_partial++;
  551.       if (nwritten != oc)
  552.     {
  553.       error (0, errno, "%s", output_file);
  554.       if (!(conversions_mask & C_NOERROR))
  555.         quit (1);
  556.     }
  557.     }
  558. }
  559.  
  560. void
  561. scanargs (argc, argv)
  562.      int argc;
  563.      char **argv;
  564. {
  565.   int i, n;
  566.  
  567.   for (i = 1; i < argc; i++)
  568.     {
  569.       char *name, *val;
  570.  
  571.       name = argv[i];
  572.       val = index (name, '=');
  573.       if (val == NULL)
  574.     usage ("unrecognized option `%s'", name);
  575.       *val++ = '\0';
  576.  
  577.       if (equal (name, "if"))
  578.     input_file = val;
  579.       else if (equal (name, "of"))
  580.     output_file = val;
  581.       else if (equal (name, "conv"))
  582.     parse_conversion (val);
  583.       else
  584.     {
  585.       n = parse_integer (val);
  586.  
  587.       if (equal (name, "ibs"))
  588.         {
  589.           input_blocksize = n;
  590.           conversions_mask |= C_HARDWAY;
  591.         }
  592.       else if (equal (name, "obs"))
  593.         {
  594.           output_blocksize = n;
  595.           conversions_mask |= C_HARDWAY;
  596.         }
  597.       else if (equal (name, "bs"))
  598.         output_blocksize = input_blocksize = n;
  599.       else if (equal (name, "cbs"))
  600.         conversion_blocksize = n;
  601.       else if (equal (name, "skip"))
  602.         skip_records = n;
  603.       else if (equal (name, "seek"))
  604.         seek_record = n;
  605.       else if (equal (name, "count"))
  606.         max_record = n;
  607.       else
  608.         usage ("unrecognized option `%s=%s'", name, val);
  609.     }
  610.     }
  611. }
  612.  
  613. int
  614. parse_integer (str)
  615.      char *str;
  616. {
  617.   int n = 0;
  618.   char *p = str;
  619.  
  620.   while (isdigit (*p))
  621.     {
  622.       n = n * 10 + *p - '0';
  623.       p++;
  624.     }
  625. loop:
  626.   switch (*p++)
  627.     {
  628.     case '\0':
  629.       return n;
  630.     case 'b':
  631.       n *= 512;
  632.       goto loop;
  633.     case 'k':
  634.       n *= 1024;
  635.       goto loop;
  636.     case 'w':
  637.       n *= 2;
  638.       goto loop;
  639.     case 'x':
  640.       n *= parse_integer (p);
  641.       break;
  642.     default:
  643.       return 0;
  644.     }
  645.   return n;
  646. }
  647.  
  648. void
  649. parse_conversion (str)
  650.      char *str;
  651. {
  652.   char *new;
  653.   int i, j;
  654.   unsigned char *new_trans;
  655.  
  656.   do
  657.     {
  658.       new = index (str, ',');
  659.       if (new != NULL)
  660.     *new++ = '\0';
  661.       for (i = 0; conversions[i].convname != NULL; i++)
  662.     if (equal (conversions[i].convname, str))
  663.       {
  664.         conversions_mask |= conversions[i].conversion;
  665.         break;
  666.       }
  667.       if (conversions[i].convname == NULL)
  668.     {
  669.       usage ("%s: invalid conversion", str);
  670.       exit (1);
  671.     }
  672. #define MX(a) (bit_count (conversions_mask & (a)))
  673.       if ((MX (C_ASCII | C_EBCDIC | C_IBM) > 1)
  674.       || (MX (C_BLOCK | C_UNBLOCK) > 1)
  675.       || (MX (C_LCASE | C_UCASE) > 1)
  676.       || (MX (C_UNBLOCK | C_SYNC) > 1))
  677.     {
  678.       error (1, 0, "\
  679. only one conv in {ascii,ebcdic,ibm}, {lcase,ucase}, {block,unblock}, {unblock,sync}");
  680.     }
  681. #undef MX
  682.       str = new;
  683.   } while (new != NULL);
  684.  
  685.   /* I don't know if the following restriction is stupid,
  686.      but it's convenient. */
  687.   if ((conversions_mask & C_SWAB) && (input_blocksize % 2 != 0
  688.                     || output_blocksize % 2 != 0))
  689.     {
  690.       error (1, 0, "ibs and obs must both be even for swab to work");
  691.     }
  692.  
  693.   /* Fix up translation table. */
  694.  
  695.   /* Do upper and lower case if necessary. */
  696.   if (conversions_mask & C_UCASE)
  697.     for (j = 'a'; j <= 'z'; j++)
  698.       trans_table[j] = toupper (j);
  699.   else if (conversions_mask & C_LCASE)
  700.     for (j = 'A'; j <= 'Z'; j++)
  701.       trans_table[j] = tolower (j);
  702.  
  703.   /* Now find and apply char set translation. */
  704.   if (conversions_mask & C_ASCII)
  705.     new_trans = ebcdic_to_ascii;
  706.   else if (conversions_mask & C_EBCDIC)
  707.     {
  708.       new_trans = ascii_to_ebcdic;
  709.       newline_character = ascii_to_ebcdic['\n'];
  710.       space_character = ascii_to_ebcdic[' '];
  711.     }
  712.   else if (conversions_mask & C_IBM)
  713.     {
  714.       new_trans = ascii_to_ibm;
  715.       newline_character = ascii_to_ibm['\n'];
  716.       space_character = ascii_to_ibm[' '];
  717.     }
  718.   else
  719.     return;
  720.  
  721.   for (i = 0; i < 256; i++)
  722.     trans_table[i] = new_trans[trans_table[i]];
  723. }
  724.  
  725. /* Return number of 1 bits in `i'. */
  726. int
  727. bit_count (i)
  728.      register int i;
  729. {
  730.   register int n;
  731.  
  732.   for (n = 0; i != 0; i = (i >> 1) & ~(1 << 31))
  733.     n += i & 1;
  734.   return n;
  735. }
  736.  
  737. /* Print statistics and exit with status `code'. */
  738. void
  739. quit (code)
  740.      int code;
  741. {
  742.   fprintf (stderr, "%u+%u records in\n", r_full, r_partial);
  743.   fprintf (stderr, "%u+%u records out\n", w_full, w_partial);
  744.   if (r_truncate > 0)
  745.     fprintf (stderr, "%u truncated records\n", r_truncate);
  746.  
  747.   exit (code);
  748. }
  749.  
  750. SIGTYPE
  751. interrupt_handler ()
  752. {
  753.   quit (1);
  754. }
  755.  
  756. /* malloc or die. */
  757. char *
  758. xmalloc (n)
  759.      int n;
  760. {
  761.   char *p;
  762.  
  763.   p = malloc (n);
  764.   if (p == NULL)
  765.     error (3, 0, "virtual memory exhausted");
  766.   return p;
  767. }
  768.  
  769. void
  770. usage (string, arg0, arg1)
  771.      char *string, *arg0, *arg1;
  772. {
  773.   fprintf (stderr, "%s: ", program_name);
  774.   fprintf (stderr, string, arg0, arg1);
  775.   fprintf (stderr, "\n");
  776.   fprintf (stderr, "\
  777. Usage: %s [if=file] [of=file] [ibs=bytes] [obs=bytes] [bs=bytes] [cbs=bytes]\n\
  778.        [skip=blocks] [seek=blocks] [count=blocks]\n\
  779.        [conv={ascii,ebcdic,ibm,block,unblock,lcase,ucase,swab,noerror,sync}]\n\
  780. Numbers can be followed by a multiplier:\n\
  781. b=512, k=1024, w=2, xm=number m\n",
  782.        program_name);
  783.   exit (1);
  784. }
  785.